import * as Cesium from "cesium"
import { DrawerBase, EventsConfig } from "./DrawerBase"

/**
 * @author Oo青花鱼
 * @description 多边形绘制
 * @example
 * //实例化Drawer
 * const polygonDrawer = new PolygonDrawer(viewer);
 * polygonDrawer.start() //开始绘制
 * polygonDrawer.end() //结束绘制
 * polygonDrawer.clear() //清空绘制的图形
 * polygonDrawer.destroy() //销毁实例
 * polygonDrawer.data //访问绘制的Entity
 * polygonDrawer.onClose = (entity:Cesium.Entity)=>{
 *    console.log('绘制了一个多边形',entity)
 * }
 */
export class PolygonDrawer extends DrawerBase {
  protected helpers: Cesium.Entity.ConstructorOptions[] = [
    //鼠标位置
    {
      position: new Cesium.CallbackPositionProperty(() => {
        return this.mousePosition
      }, false),
      point: {},
    },
    //动态多边形
    {
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length === 1) {
            //线段
            return [...this.featurePositions.value, this.mousePosition]
          } else if (
            this.mousePosition &&
            this.featurePositions.value.length > 1
          ) {
            //闭合多边形
            return [
              ...this.featurePositions.value,
              this.mousePosition,
              this.featurePositions.value[0],
            ]
          } else if (this.featurePositions.value.length >= 3) {
            return [...this.featurePositions.value]
          } else return undefined
        }, false),
        clampToGround: true,
      },
      polygon: {
        hierarchy: new Cesium.CallbackProperty(() => {
          if (this.featurePositions.value.length >= 2 && this.mousePosition) {
            //闭合多边形
            return new Cesium.PolygonHierarchy(
              [...this.featurePositions.value, this.mousePosition].map((item) =>
                item.clone(),
              ),
            )
          } else if (this.featurePositions.value.length >= 3) {
            return new Cesium.PolygonHierarchy([
              ...this.featurePositions.value.map((item) => item.clone()),
            ])
          } else return undefined
        }, false),
      },
    },
  ]
  protected events: EventsConfig = {
    cesiumEvents: [
      {
        type: "click",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          //拾取顶点
          const position = this.scene.pickPosition(e.position)
          this.featurePositions.value.push(position)
        },
      },
      {
        type: "rightclick",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          //拾取顶点
          const position = this.scene.pickPosition(e.position)
          this.featurePositions.value.push(position)

          //根据顶点绘制图形
          this.drawFeature({
            polygon: {
              hierarchy: new Cesium.PolygonHierarchy(
                this.featurePositions.value,
              ),
            },
          })

          //初始化图形绘制状态
          this.featurePositions.value = []
        },
      },
    ],
  }
}
